package com.example.zhinongbao.view;//package com.example.zhinongbao.view;
//
//import android.content.Intent;
//import android.graphics.Bitmap;
//import android.net.Uri;
//import android.os.Bundle;
//import android.provider.MediaStore;
//import android.transition.Slide;
//import android.view.View;
//import android.widget.Button;
//import android.widget.EditText;
//
//import androidx.activity.result.ActivityResult;
//import androidx.activity.result.ActivityResultCallback;
//import androidx.activity.result.ActivityResultLauncher;
//import androidx.activity.result.contract.ActivityResultContracts;
//import androidx.annotation.Nullable;
//import androidx.appcompat.app.AppCompatActivity;
//import androidx.recyclerview.widget.GridLayoutManager;
//import androidx.recyclerview.widget.RecyclerView;
//
//
//import com.example.zhinongbao.R;
//import com.example.zhinongbao.adapter.GoodsPhotoAdapter;
//import com.example.zhinongbao.contract.AddGoodsContract;
//import com.example.zhinongbao.entity.Goods;
//import com.example.zhinongbao.entity.Photo;
//import com.example.zhinongbao.presenter.AddGoodsPresenter;
//
//
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
//
//public class AddGoodsActivity extends AppCompatActivity implements
//        GoodsPhotoAdapter.OnPhotoListener, AddGoodsContract.IAddGoodsView, View.OnClickListener {
//
//   private RecyclerView mPhotoRecyclerView;
//   private EditText mGoodsNameEdit;
//   private Button mSendButton;
//   private List<Photo> mPhotoList;
//   private GoodsPhotoAdapter mGoodsPhotoAdapter;
//   private ActivityResultLauncher mActivityResultLauncher;
//   private AddGoodsContract.IAddGoodsPresenter mAddGoodsPresenter;
//
//   @Override
//   protected void onCreate(@Nullable Bundle savedInstanceState) {
//      super.onCreate(savedInstanceState);
//      getWindow().setEnterTransition(new Slide());
//      setContentView(R.layout.activity_add_goods);
//      mAddGoodsPresenter = new AddGoodsPresenter(this, this);
//      mPhotoList = new ArrayList<>();
//      mPhotoList.add(new Photo());
//      mPhotoRecyclerView = findViewById(R.id.rv_goods_photo);
//      mGoodsNameEdit = findViewById(R.id.et_goods_name);
//      mSendButton = findViewById(R.id.goods_send);
//      mSendButton.setOnClickListener(this);
//      GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
//      gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
//      mPhotoRecyclerView.setLayoutManager(gridLayoutManager);
//      mGoodsPhotoAdapter = new GoodsPhotoAdapter(mPhotoList);
//      mGoodsPhotoAdapter.setPhotoListener(this);
//      mPhotoRecyclerView.setAdapter(mGoodsPhotoAdapter);
//      onActivityResult();
//   }
//
//   @Override
//   public void onAdd() {
//      Intent intent = new Intent();
//      intent.setType("image/*");
//      intent.setAction(Intent.ACTION_GET_CONTENT);
//      mActivityResultLauncher.launch(intent);
//   }
//
//   @Override
//   public void onDelete(int position) {
//      //暂时没用
//   }
//
//   private void onActivityResult() {
//      mActivityResultLauncher = registerForActivityResult(
//              new ActivityResultContracts.StartActivityForResult(),
//              new ActivityResultCallback<ActivityResult>() {
//                 @Override
//                 public void onActivityResult(ActivityResult result) {
//                    int resultCode = result.getResultCode();
//                    if (resultCode == RESULT_OK) {
//                       Intent data = result.getData();
//                       Uri selectedImageUri = data.getData();
//                       try {
//                          // 使用图片URI进行处理，例如加载到ImageView中
//                          Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
//                          Photo photo = new Photo();
//                          photo.setBitmap(bitmap);
//                          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//                          Bitmap resized = Bitmap.createScaledBitmap(bitmap, (int)(bitmap.getWidth()*0.5), (int)(bitmap.getHeight()*0.5), true);
//                          resized.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
//                          photo.setData(outputStream.toByteArray());
//                          mGoodsPhotoAdapter.addPhoto(photo);
//                       } catch (IOException e) {
//                          e.printStackTrace();
//                       }
//                    }
//                 }
//              });
//   }
//
//   @Override
//   public void showGoodsList(List<Goods> goodsList) {
//
//   }
//
//   @Override
//   public void onClick(View v) {
//      Goods goods = new Goods();
//      goods.setName(mGoodsNameEdit.getText().toString());
//      goods.setPrice(3.21);
//      goods.setUnit("两");
//      goods.setMessage("好吃好吃太好吃了");
//      goods.setPhotoList(mGoodsPhotoAdapter.getPhotoList());
//      mAddGoodsPresenter.saveGoods(goods);
//   }
//}


import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.transition.Slide;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

//import com.jlastu.znb.R;
//import com.jlastu.znb.adapter.GoodsPhotoAdapter;
//import com.jlastu.znb.contract.AddGoodsContract;
//import com.jlastu.znb.entity.Goods;
//import com.jlastu.znb.entity.Photo;
//import com.jlastu.znb.presenter.AddGoodsPresenter;

import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.GoodsPhotoAdapter;
import com.example.zhinongbao.contract.AddGoodsContract;
import com.example.zhinongbao.entity.Goods;
import com.example.zhinongbao.entity.Photo;
import com.example.zhinongbao.presenter.AddGoodsPresenter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AddGoodsActivity extends AppCompatActivity implements
        GoodsPhotoAdapter.OnPhotoListener, AddGoodsContract.IAddGoodsView, View.OnClickListener {

   private RecyclerView mPhotoRecyclerView;
   private EditText mGoodsNameEdit;
   private Button mSendButton;
   private List<Photo> mPhotoList;
   private GoodsPhotoAdapter mGoodsPhotoAdapter;
   private ActivityResultLauncher mActivityResultLauncher;
   private AddGoodsContract.IAddGoodsPresenter mAddGoodsPresenter;

   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      getWindow().setEnterTransition(new Slide());
      setContentView(R.layout.activity_add_goods);
      mAddGoodsPresenter = new AddGoodsPresenter(this, this);
      mPhotoList = new ArrayList<>();
      mPhotoList.add(new Photo());
      mPhotoRecyclerView = findViewById(R.id.rv_goods_photo);
      mGoodsNameEdit = findViewById(R.id.et_goods_name);
      mSendButton = findViewById(R.id.goods_send);
      mSendButton.setOnClickListener(this);
      GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
      gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
      mPhotoRecyclerView.setLayoutManager(gridLayoutManager);
      mGoodsPhotoAdapter = new GoodsPhotoAdapter(mPhotoList);
      mGoodsPhotoAdapter.setPhotoListener(this);
      mPhotoRecyclerView.setAdapter(mGoodsPhotoAdapter);
      onActivityResult();
   }

   @Override
   public void onAdd() {
      Intent intent = new Intent();
      intent.setType("image/*");
      intent.setAction(Intent.ACTION_GET_CONTENT);
      mActivityResultLauncher.launch(intent);
   }

   @Override
   public void onDelete(int position) {
      //暂时没用
   }

   private void onActivityResult() {
      mActivityResultLauncher = registerForActivityResult(
              new ActivityResultContracts.StartActivityForResult(),
              new ActivityResultCallback<ActivityResult>() {
                 @Override
                 public void onActivityResult(ActivityResult result) {
                    int resultCode = result.getResultCode();
                    if (resultCode == RESULT_OK) {
                       Intent data = result.getData();
                       Uri selectedImageUri = data.getData();
                       try {
                          // 使用图片URI进行处理，例如加载到ImageView中
                          Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
                          Photo photo = new Photo();
                          photo.setBitmap(bitmap);
                          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                          Bitmap resized = Bitmap.createScaledBitmap(bitmap, (int)(bitmap.getWidth()*0.4), (int)(bitmap.getHeight()*0.4), true);
                          resized.compress(Bitmap.CompressFormat.JPEG, 60, outputStream);
                          photo.setData(outputStream.toByteArray());
                          mGoodsPhotoAdapter.addPhoto(photo);
                       } catch (IOException e) {
                          e.printStackTrace();
                       }
                    }
                 }
              });
   }

   @Override
   public void showGoodsList(List<Goods> goodsList) {

   }

   @Override
   public void onClick(View v) {
      if (v.getId() == R.id.goods_send){
         Goods goods = new Goods();
         goods.setName(mGoodsNameEdit.getText().toString());
         goods.setPrice(3.21);
         goods.setUnit("两");
         goods.setMessage("好吃好吃太好吃了");
         goods.setPhotoList(mGoodsPhotoAdapter.getPhotoList());
         mAddGoodsPresenter.saveGoods(goods);
         Toast.makeText(this, "发布成功", Toast.LENGTH_SHORT).show();
         finish();
      }
   }
}
